//
//  HomeCell.swift
//  weibo
//
//  Created by tomato on 2023/2/24.
//

import UIKit
import SnapKit

class HomeCell: UITableViewCell {
   
    //一个类A如果有自己独有的初始化方法，自定义类B的时候如果B继承自A。此时就重写A类独有的初始化方法
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier:reuseIdentifier )
        self.selectionStyle = .none//设置cell点击效果
        setupViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 懒加载
    //contentL 内容
    private lazy var contentL: UILabel = {
        let contentL = UILabel()
        // 设置label大小
        contentL.font = UIFont.systemFont(ofSize: 14)
        // 设置颜色
        contentL.textColor = UIColor.black
        contentL.numberOfLines = 0
        return contentL
    }()
    //contentL 内容View
    private lazy var contentTextView: UIView = {
        let contentTextView = UIView()
        contentTextView.addSubview(contentL)
        contentL.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(15)
            make.right.equalToSuperview().offset(-15)
            make.top.bottom.equalToSuperview().offset(0)
        }
        return contentTextView
    }()
    
    //用户信息view
    private lazy var userInfoView: UserInfoView = UserInfoView()
    
    //UIStackView容器
    private lazy var stackView: UIStackView = {
        let stackView = UIStackView()
        stackView.axis = .vertical
        return stackView
    }()
    
    //配图
    private lazy var picturesView: PicturesView = PicturesView()
    
    //底部工具栏
    private lazy var bottomView: HomeBottomView = HomeBottomView()
    
    // MARK: - 赋值
    var viewModel : StatusViewModel?{
        didSet{
            guard let viewModel = viewModel else {
                return
            }
            userInfoView.headImageView.set_Image(image: viewModel.status?.user?.profile_image_url)
            userInfoView.nameL.text = viewModel.status?.user?.screen_name
            // 来源
            userInfoView.sourceLabel.text = viewModel.sourceStr
            // 发布时间
            userInfoView.timeLabel.text = viewModel.timeStr
            contentL.text = viewModel.status?.text
            //判断图片数组是否有值
            if let picUrls = viewModel.status?.pic_urls, picUrls.count > 0{
                picturesView.isHidden = false
                picturesView.pictureArray = viewModel.status?.pic_urls
            }else{
                picturesView.isHidden = true
            }
        }
    }
}

// MARK: - UI
extension HomeCell{
    private func setupViews(){
        //控件加到contentView上
        self.contentView.addSubview(userInfoView)
       
        self.contentView.addSubview(stackView)
        stackView.addArrangedSubview(contentTextView)
        stackView.addArrangedSubview(picturesView)
        
        self.contentView.addSubview(bottomView)
        userInfoView.snp.makeConstraints { (make) in
            make.left.right.top.equalToSuperview().offset(0)
            make.height.equalTo(53)
        }
        
        picturesView.snp.makeConstraints { (make) in
            make.height.equalTo(100)
        }
        
        stackView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview().offset(0)
            make.top.equalTo(userInfoView.snp_bottom).offset(5)
        }
        
        bottomView.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(0)
            make.right.equalToSuperview().offset(0)
            make.top.equalTo(stackView.snp.bottom).offset(5)
            make.height.equalTo(40)
            make.bottom.equalToSuperview().offset(0)
        }
    }
}
// MARK: - 交互
extension HomeCell{
    @objc private func followBtnClick(){
       
    }
}
// MARK: - 设置
extension HomeCell{
   
}
